package com.JobWebGem.system.service.impl;

import com.JobWebGem.system.mapper.CompanyMapper;
import com.JobWebGem.system.model.dto.company.CompanyQueryRequest;
import com.JobWebGem.system.model.entity.Company;
import com.JobWebGem.system.model.vo.CompanyVO;
import com.JobWebGem.system.service.CompanyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author renli
 * @description 针对表【company(存储公司信息)】的数据库操作Service实现
 * @createDate 2024-12-02 21:18:40
 */
@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company>
        implements CompanyService {

    @Resource
    private CompanyMapper companyMapper;

    @Override
    public Page<CompanyVO> getAll(Page<Company> page, CompanyQueryRequest companyQueryRequest) {
        QueryWrapper<Company> queryWrapper = new QueryWrapper<>();
        if (companyQueryRequest.getName() != null && !companyQueryRequest.getName().equals("")) {
            queryWrapper.like("name", companyQueryRequest.getName());
        }
        if (companyQueryRequest.getIntroduction() != null && !companyQueryRequest.getIntroduction().equals("")) {
            queryWrapper.like("introduction", companyQueryRequest.getIntroduction());
        }
        queryWrapper.orderByDesc("id");
        Page<Company> companyPage = companyMapper.selectPage(page, queryWrapper);

        // 转换为 CompanyVO
        Page<CompanyVO> companyVOPage = new Page<>(companyPage.getCurrent(), companyPage.getSize(),
                companyPage.getTotal());
        List<CompanyVO> companyVOList = companyPage.getRecords().stream().map(company -> {
            CompanyVO companyVO = new CompanyVO();
            BeanUtils.copyProperties(company, companyVO);
            // 转换上市状态
            companyVO.setListedStatus(company.getListedStatus() == 0 ? "未上市" : "上市");
            return companyVO;
        }).collect(Collectors.toList());
        companyVOPage.setRecords(companyVOList);

        return companyVOPage;
    }
}
